package com.iweb.service;

import com.common.datasource.DataSourceUtil;
import com.common.model.DataGrid;
import com.common.model.Page;
import com.iweb.dao.AdminDao;
import com.iweb.dao.AdminDaoImpl;
import com.iweb.dao.UserDao;
import com.iweb.dao.UserDaoImpl;
import com.iweb.pojo.Admin;
import com.iweb.pojo.User;

import java.util.List;
import java.util.UUID;

public class AdminService {

    public Admin findUserByUsername(String admin_name) {
        Admin admin = null;
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        AdminDao adminDao = new AdminDaoImpl(dataSourceUtil.getConnection());

        try {
            // 业务逻辑
            admin = adminDao.findUserByUsername(admin_name);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dataSourceUtil.close();
        }
        return admin;
    }

    public DataGrid<User> findUserList(Page page) {
        DataGrid<User> dataGrid = new DataGrid<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        UserDao userDao = new UserDaoImpl(dataSourceUtil.getConnection());

        try {

            long count =userDao.selectCount();
            List<User> list = userDao.findAll(page);
            dataGrid.setCode(0);
            dataGrid.setMsg("");
            dataGrid.setCount(count);
            dataGrid.setData(list);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return dataGrid;
    }
    public DataGrid<User> findOrderList(Page page) {
        DataGrid<User> dataGrid = new DataGrid<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        UserDao userDao = new UserDaoImpl(dataSourceUtil.getConnection());

        try {

            long count =userDao.selectCount();
            List<User> list = userDao.findAll(page);
            dataGrid.setCode(0);
            dataGrid.setMsg("");
            dataGrid.setCount(count);
            dataGrid.setData(list);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return dataGrid;
    }

    // 方法同步的
    public synchronized boolean addAdmin(User users) {

        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        UserDao userDao = new UserDaoImpl(dataSourceUtil.getConnection());

        try {
            User user = userDao.findByUsername(users.getUser_name());
            // 验证用户是否可以注册
            if(user != null){
                throw new RuntimeException();
            }
            // 注册用户
            // 设定主键
            users.setUser_id(UUID.randomUUID().toString());
            userDao.save(users);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            dataSourceUtil.close();
        }
    }

    public void deleteById(String user_id) {
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        UserDao userDao = new UserDaoImpl(dataSourceUtil.getConnection());

        try {
            userDao.deleteById(user_id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
    }

    public User findById(String user_id) {
        User user = null;
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        UserDao userDao = new UserDaoImpl(dataSourceUtil.getConnection());

        try {
            user = userDao.findById(user_id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return user;
    }
}
